package com.jeesite.modules.task.receive.web;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.backlog.service.DcSupBacklogService;
import com.jeesite.modules.common.service.SysMsgService;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.home.entity.DcSupTaskActionExplain;
import com.jeesite.modules.msg.entity.content.PcMsgContent;
import com.jeesite.modules.msg.utils.MsgPushUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoAdd;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedback;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedbackProvince;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedbackProvinceExt;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedbackReject;
import com.jeesite.modules.task.feedback.service.DcSupTaskFeedbackService;
import com.jeesite.modules.task.receive.entity.TaskReceiveDto;
import com.jeesite.modules.task.receive.service.TaskReceiveService;
import com.jeesite.modules.task.status.service.DcSupTaskStatusService;
import com.jeesite.modules.utils.Dict;
import com.jeesite.modules.utils.OfficeUtil;
import com.jeesite.modules.yzw.entity.MsgDetail;
import com.jeesite.modules.yzw.service.LehandYzwPushTask;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 任务过程审核表Controller
 * @author zrr
 * @version 2022-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/audit/taskReceive")
public class TaskReceiveController extends BaseController {

	@Autowired
	private TaskReceiveService taskReceiveService;
	@Autowired
	private DcSupTaskActionService dcSupTaskActionService;
	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	@Autowired
	private DcSupTaskFeedbackService dcSupTaskFeedbackService;
	@Autowired
	private DcSupTaskStatusService dcSupTaskStatusService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	@Autowired
	private LehandYzwPushTask lehandYzwPushTask;
	@Autowired
	private SysMsgService sysMsgService;
	@Autowired
	private DcSupBacklogService dcSupBacklogService;


	/**
	 * 查询列表
	 */
	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = {"list", ""})
	public String list(TaskReceiveDto taskReceive, Model model) {
		taskReceive.setTaskYear(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(null));
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("taskReceive", taskReceive);
		return "modules/task/receive/taskReceiveList";
	}
	@RequestMapping(value = {"feedbackListDelete", ""})
	public String feedbackListDelete(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("feedbackList", taskReceive);
		return "modules/task/delete/feedbackListDelete";
	}

	@RequestMapping(value = {"feedbackForm", ""})
	public String feedbackForm(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("feedbackForm", taskReceive);
		return "modules/task/delete/feedbackForm";
	}

	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = {"list1", ""})
	public String list1(TaskReceiveDto taskReceive, Model model) {
		taskReceive.setTaskYear(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(null));
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("taskReceive", taskReceive);
		return "modules/task/receive/taskReceiveList1";
	}

	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = {"list2", ""})
	public String list2(TaskReceiveDto taskReceive, Model model) {
		taskReceive.setTaskYear(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(null));
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("taskReceive", taskReceive);
		return "modules/task/receive/taskReceiveList2";
	}

	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = {"list3", ""})
	public String list3(TaskReceiveDto taskReceive, Model model) {
		taskReceive.setTaskYear(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(null));
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		model.addAttribute("taskReceive", taskReceive);
		return "modules/task/receive/taskReceiveList3";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TaskReceiveDto> listData(TaskReceiveDto taskReceive, HttpServletRequest request, HttpServletResponse response) {
		taskReceive.setPage(new Page<>(request, response));
		if(StringUtils.isNotBlank(taskReceive.getTaskStatus())&&taskReceive.getTaskStatus().contains(",")){
			taskReceive.setTaskStatus(taskReceive.getTaskStatus().replace(",",""));
		}
		String orderBy = taskReceive.getOrderBy()==null?"0":taskReceive.getOrderBy();
		if(StringUtils.isBlank(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("3");
		}
		//待接收列表按照督办字号排序
		if ("1".equals(taskReceive.getTaskStatus())){
			taskReceive.setOrderBy("1");
		}
		if("messageNumber desc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("1");
		}else if("messageNumber asc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("0");
		}else if("receiveTime desc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("3");
		}else if("receiveTime asc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("2");
		}else if("feedbackEndTimeStr desc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("5");
		}else if("feedbackEndTimeStr asc".equals(taskReceive.getOrderBy())){
			taskReceive.setOrderBy("4");
		}
		// 当前登录人【所属部门】是【督办单位】；system查看所有
		taskReceive.setLeaderOrgCode(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());

		Page<TaskReceiveDto> page = taskReceiveService.findPage(taskReceive);

		Map<String,List<DcSupTaskFeedback>> resultMap=null;

		if (!CollectionUtils.isEmpty(page.getList())){
			 List<String> ids = page.getList().stream().map(s -> s.getId()).collect(Collectors.toList());
			 if (CollectionUtils.isEmpty(ids)){
			 	resultMap =new HashMap<>();
			 }else{
			 	resultMap = taskReceiveService.getFeedbackListByIds(ids);
			 }
		}
		if (!CollectionUtils.isEmpty(resultMap)){
			for (int i = 0; i < page.getList().size(); i++) {
				//查询是否有反馈
				 TaskReceiveDto receiveDto = page.getList().get(i);
				String id = receiveDto.getId();
				if (resultMap.containsKey(id)){
					List<DcSupTaskFeedback> list = resultMap.get(id);
					long isFeedback = list.stream().filter(s->s.getFeedbackTime()!=null).count();
					List<DcSupTaskFeedback> listFeedback = list.stream().filter(s->!Dict.FeedbackStatus.STATUS_2.equals(s.getFeedbackStatus())&&!Dict.FeedbackStatus.STATUS_3.equals(s.getFeedbackStatus())&&!Dict.FeedbackStatus.STATUS_5.equals(s.getFeedbackStatus())&&!Dict.FeedbackStatus.STATUS_9.equals(s.getFeedbackStatus())).collect(Collectors.toList());
					receiveDto.setIsFeedback(isFeedback);
					if(listFeedback.size()!=0){
						if(listFeedback.get(0).getFeedbackEndTime().getTime()< System.currentTimeMillis()){
							receiveDto.setColour("red");
						}
						if(!"11".equals(listFeedback.get(0).getFeedbackStatus())){
							receiveDto.setFeedbackEndTimeStr(listFeedback.get(0).getFeedbackEndTimeStr());
						}
					}
				}
			}
		}
		//按照任务状态排序
		page.setList(page.getList());
		return page;

	}

	/**
	 *任务反馈历史
	 */
	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "listDataHis")
	@ResponseBody
	public Page<TaskReceiveDto> listDataHis(TaskReceiveDto taskReceive, HttpServletRequest request, HttpServletResponse response) {
		taskReceive.setPage(new Page<>(request, response));
		Page<TaskReceiveDto> page = taskReceiveService.listDataHis(taskReceive);
		return page;
	}

	@RequestMapping(value = "feedbackListData")
	@ResponseBody
	public Page<TaskReceiveDto> feedbackListData(TaskReceiveDto taskReceive, HttpServletRequest request, HttpServletResponse response) {
		taskReceive.setPage(new Page<>(request, response));
		Page<TaskReceiveDto> page = taskReceiveService.feedbackListData(taskReceive);
		return page;
	}

	/**
	 * 反馈页面
	 * @param taskReceive
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "taskReceiveOne")
	public String taskReceiveOne(DcSupTaskFeedback taskReceive, Model model) {
		//获取反馈表数据
		DcSupTaskFeedback dcSupTaskFeedback = new DcSupTaskFeedback();
		dcSupTaskFeedback.setId(taskReceive.getId());
		DcSupTaskFeedback dcSupTaskFeedbackRes = dcSupTaskFeedbackService.getOverRide(dcSupTaskFeedback);
		// 判断是否已提交
		if(dcSupTaskFeedbackRes!=null&&(Dict.FeedbackStatus.STATUS_2.equals(dcSupTaskFeedbackRes.getFeedbackStatus())||Dict.FeedbackStatus.STATUS_3.equals(dcSupTaskFeedbackRes.getFeedbackStatus())||Dict.FeedbackStatus.STATUS_5.equals(dcSupTaskFeedbackRes.getFeedbackStatus())||Dict.FeedbackStatus.STATUS_9.equals(dcSupTaskFeedbackRes.getFeedbackStatus()))){
			taskReceiveOneView(taskReceive,model);

			return "modules/task/receive/taskReceiveOneView";
		}else{
			//返回右边数据
			DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
			dcSupTaskInfo.setId(dcSupTaskFeedbackRes.getTaskId());
			DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskInfoService.get(dcSupTaskInfo);
			//看是省办数据是否存在
			String platformJson = dcSupTaskInfoRes.getPlatformJson();
			if(StringUtils.equals("999",dcSupTaskInfoRes.getTaskTypeCode())) {
				if (StringUtils.isNotBlank(platformJson)) {
					Map map = (Map) JSONObject.parse(platformJson);
					String bizId = (String) map.get("bizId");
					String reportEndDate = (String) map.get("reportEndDate");
					DcSupTaskFeedbackProvinceExt dcSupTaskFeedbackProvince = new DcSupTaskFeedbackProvinceExt();
					dcSupTaskFeedbackProvince.setBizId(bizId);
					dcSupTaskFeedbackProvince.setActionId(dcSupTaskFeedbackRes.getActionId());
					dcSupTaskFeedbackProvince.setTaskId(dcSupTaskFeedbackRes.getTaskId());
					dcSupTaskFeedbackProvince.setId(dcSupTaskFeedbackRes.getId());
					dcSupTaskFeedbackProvince.setReportDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
					dcSupTaskFeedbackProvince.setReportEndDate(reportEndDate);
					model.addAttribute("dcSupTaskFeedbackProvince", dcSupTaskFeedbackProvince);
					return "modules/task/receive/taskReceiveProvince";
				}else{
					DcSupTaskFeedbackProvinceExt dcSupTaskFeedbackProvince = new DcSupTaskFeedbackProvinceExt();
					dcSupTaskFeedbackProvince.setBizId(dcSupTaskInfoRes.getId());
					dcSupTaskFeedbackProvince.setActionId(dcSupTaskFeedbackRes.getActionId());
					dcSupTaskFeedbackProvince.setTaskId(dcSupTaskFeedbackRes.getTaskId());
					dcSupTaskFeedbackProvince.setId(dcSupTaskFeedbackRes.getId());
					dcSupTaskFeedbackProvince.setReportDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
					dcSupTaskFeedbackProvince.setReportEndDate(DateUtils.formatDate(dcSupTaskFeedbackRes.getFeedbackEndTime(), "yyyy-MM-dd HH:mm:ss"));
					dcSupTaskFeedbackProvince.setDecomposeFlag("1");
					model.addAttribute("dcSupTaskFeedbackProvince", dcSupTaskFeedbackProvince);

					return "modules/task/receive/taskReceiveProvince";
				}
			}
			//第一次进入设置反馈人和反馈人电话数据
			if("-1".equals(dcSupTaskFeedbackRes.getFeedbackStatus())){
				dcSupTaskFeedbackRes.setFeedbackName(UserUtils.getUser().getUserName());
				dcSupTaskFeedbackRes.setFeedbackPhone(UserUtils.getUser().getMobile());
			}

			//设置处理后显示的反馈时间
			dcSupTaskFeedbackRes.setFeedbackEndTimeStr(DateUtils.formatDate(dcSupTaskFeedbackRes.getFeedbackEndTime(),"yyyy-MM-dd HH:mm"));

			model.addAttribute("taskReceiveOne", dcSupTaskFeedbackRes);

			dcSupTaskInfoRes.setRemarks2(OfficeUtil.getTaskRequire(dcSupTaskInfoRes.getFeedbackWay(),dcSupTaskInfoRes.getFeedbackRate()));
			model.addAttribute("dcSupTaskInfoRes", dcSupTaskInfoRes);
			//返回中间数据
			DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
			dcSupTaskAction.setId(dcSupTaskFeedbackRes.getActionId());
			DcSupTaskAction dcSupTaskActionRes = dcSupTaskActionService.get(dcSupTaskAction);
			dcSupTaskFeedback.setActionId(dcSupTaskFeedbackRes.getActionId());
			Integer overTime = dcSupTaskFeedbackService.queryOverTimeCount(dcSupTaskFeedback);
			dcSupTaskActionRes.setOverTime(overTime);
			model.addAttribute("dcSupTaskActionRes", dcSupTaskActionRes);
			//首先判断是不是细化任务,是不是按照季度反馈
			if(StringUtils.equals(dcSupTaskInfoRes.getNormalFlag(),"3")&&StringUtils.equals(dcSupTaskInfoRes.getFeedbackWay(),"1")){
				String str = isXiHua(dcSupTaskAction.getId(),dcSupTaskFeedbackRes.getId());
				model.addAttribute("isxihua", str);
			}else{
				model.addAttribute("isxihua", "1");
			}


			 return "modules/task/receive/taskReceiveOne";
		}
	}

	private String isXiHua(String actionId,String feedbackId) {
		//获取反馈列表
		List<DcSupTaskFeedback> list = taskReceiveService.getfeedbackListById(actionId,feedbackId);
		Integer index = 1;
		for (int i = 0; i < list.size(); i++) {
			if(StringUtils.equals(list.get(i).getId(),feedbackId)){
				break;
			}
			index++;
		}
		String returnStr = "0";
		//如果相等,则允许编辑完成进度
		if(index==list.size()){
			returnStr="1";
		}
		return returnStr;
	}

	//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveOneView")
	public String taskReceiveOneView(DcSupTaskFeedback taskReceive, Model model) {
		//获取反馈表数据
		DcSupTaskFeedback dcSupTaskFeedback = new DcSupTaskFeedback();
		dcSupTaskFeedback.setId(taskReceive.getId());
		DcSupTaskFeedback dcSupTaskFeedbackRes = dcSupTaskFeedbackService.getOverRide(dcSupTaskFeedback);
		if(Dict.FeedbackStatus.STATUS_1.equals(dcSupTaskFeedbackRes.getFeedbackStatus())&&Dict.FeedbackStatus.STATUS_0.equals(taskReceive.getRemarks2())){
			DcSupTaskFeedback res = new DcSupTaskFeedback();
			res.setId("");
			res.setTaskId(dcSupTaskFeedbackRes.getTaskId());
			res.setActionId(dcSupTaskFeedbackRes.getActionId());
			res.setFeedbackStatus(dcSupTaskFeedbackRes.getFeedbackStatus());
			dcSupTaskFeedbackRes = res;
		}
		String provinceJson = dcSupTaskFeedbackRes.getProvinceJson();
		if(StringUtils.isNotBlank(provinceJson)){
			DcSupTaskFeedbackProvince dcSupTaskFeedbackProvince =  JSONObject.parseObject(provinceJson,DcSupTaskFeedbackProvince.class);
			if(StringUtils.isBlank(dcSupTaskFeedbackProvince.getReportDate())){
				DcSupTaskFeedbackReject dcSupTaskFeedbackReject =JSONObject.parseObject(provinceJson,DcSupTaskFeedbackReject.class);
				model.addAttribute("dcSupTaskFeedbackReject",dcSupTaskFeedbackReject);
				return "modules/task/receive/taskReceiveRejectView";
			}
			model.addAttribute("dcSupTaskFeedbackProvince",dcSupTaskFeedbackProvince);
			return "modules/task/receive/taskReceiveProvinceView";
		}
		//设置处理后显示的反馈时间
		dcSupTaskFeedbackRes.setFeedbackEndTimeStr(DateUtils.formatDate(dcSupTaskFeedbackRes.getFeedbackEndTime(),"yyyy-MM-dd HH:mm"));
		model.addAttribute("taskReceiveOne", dcSupTaskFeedbackRes);
		//返回右边数据
		DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
		dcSupTaskInfo.setId(dcSupTaskFeedbackRes.getTaskId());
		DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskInfoService.get(dcSupTaskInfo);
		dcSupTaskInfoRes.setRemarks2(OfficeUtil.getTaskRequire(dcSupTaskInfoRes.getFeedbackWay(),dcSupTaskInfoRes.getFeedbackRate()));
		//修改任务进度按钮权限
		//创建人可修改,机关党组成员审核通过或科室审核通过可修改,20220831增加秘书科指定科室可修改
		if((Dict.FeedbackStatus.STATUS_5.equals(dcSupTaskFeedbackRes.getFeedbackStatus())||Dict.FeedbackStatus.STATUS_9.equals(dcSupTaskFeedbackRes.getFeedbackStatus()))){
			if(UserUtils.getUser().getUserCode().equals(dcSupTaskInfoRes.getCreateBy())||StringUtils.equals(OfficeUtil.getCurrentOffice().getOfficeCode(),dcSupTaskInfoRes.getTaskSupOrg())){
				dcSupTaskInfoRes.setRemarks3(Dict.FeedbackStatus.STATUS_1);
			}else{
				dcSupTaskInfoRes.setRemarks3(Dict.FeedbackStatus.STATUS_0);
			}
		}else{
			dcSupTaskInfoRes.setRemarks3(Dict.FeedbackStatus.STATUS_0);
		}
		model.addAttribute("dcSupTaskInfoRes", dcSupTaskInfoRes);
		//返回中间数据
		DcSupTaskAction dcSupTaskAction = new DcSupTaskAction();
		dcSupTaskAction.setId(dcSupTaskFeedbackRes.getActionId());
		DcSupTaskAction dcSupTaskActionRes = dcSupTaskActionService.get(dcSupTaskAction);
		dcSupTaskFeedback.setActionId(dcSupTaskFeedbackRes.getActionId());
		dcSupTaskFeedback.setRemarks2("-1");
		Integer overTime = dcSupTaskFeedbackService.queryOverTimeCount(dcSupTaskFeedback);
		dcSupTaskActionRes.setOverTime(overTime);
		model.addAttribute("dcSupTaskActionRes", dcSupTaskActionRes);

		return "modules/task/receive/taskReceiveOneView";
	}

	/**
	 *领导批示页面
	 */
	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveTwo")
	public String taskReceiveTwo(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("taskReceiveTwo", taskReceive);
		return "modules/task/receive/taskReceiveTwo";
	}

	/**
	 * 查看反馈历史页面
	 */
//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveThree")
	public String taskReceiveThree(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("taskReceiveThree", taskReceive);
		return "modules/task/receive/taskReceiveThree";
	}
	/**
	 * 工作安排页面
	 */
//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveFour")
	public String taskReceiveFour(TaskReceiveDto taskReceive, Model model) {
		Office currentOffice = sysOfficeUnitService.getLatestUnitByOffice();
		String parentCode = currentOffice.getOfficeCode();
		String parentName = currentOffice.getOfficeName();
		model.addAttribute("parentCode", parentCode);
		model.addAttribute("parentName", parentName);
		taskReceive.setLeaderOrgCode(currentOffice.getOfficeCode());
		model.addAttribute("taskReceiveFour", taskReceive);
		return "modules/task/receive/taskReceiveFour";
	}

	/**
	 *反馈历史页面
	 */
	@RequestMapping(value = "taskReceiveFive")
	public String taskReceiveFive(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("taskReceiveFive", taskReceive);
		model.addAttribute("nowDate", new Date());
		return "modules/task/receive/taskReceiveFive";
	}
	/**
	 *反馈历史页面
	 */
//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveSix")
	public String taskReceiveSix(TaskReceiveDto taskReceive, Model model) {

		model.addAttribute("taskReceiveSix", dcSupTaskFeedbackService.get(taskReceive.getId()));
		return "modules/task/receive/taskReceiveSix";
	}
	/**
	 *反馈历史页面
	 */
//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "taskReceiveSeven")
	public String taskReceiveSeven(TaskReceiveDto taskReceive, Model model) {
		model.addAttribute("taskReceiveSeven", taskReceive);
		return "modules/task/receive/taskReceiveSeven";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@PostMapping(value = "save")
	@ResponseBody
	@RepeatSubmit
	public String save(@Validated TaskReceiveDto taskReceive) {
		taskReceiveService.save(taskReceive);
		return renderResult(Global.TRUE, text("保存任务过程审核表成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TaskReceiveDto taskReceive) {
		taskReceive.setStatus(TaskReceiveDto.STATUS_DISABLE);
		taskReceiveService.updateStatus(taskReceive);
		return renderResult(Global.TRUE, text("停用任务过程审核表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TaskReceiveDto taskReceive) {
		taskReceive.setStatus(TaskReceiveDto.STATUS_NORMAL);
		taskReceiveService.updateStatus(taskReceive);
		return renderResult(Global.TRUE, text("启用任务过程审核表成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TaskReceiveDto taskReceive) {
		taskReceiveService.delete(taskReceive);
		return renderResult(Global.TRUE, text("删除任务过程审核表成功！"));
	}

	/**
	 * 任务接收-接收
	 * @param taskReceive
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "receive")
	@ResponseBody
	@RepeatSubmit
	public String receive(TaskReceiveDto taskReceive) {
		// 用户接收时，将此处的多条消息置为已读
		DcSupTaskAction tempTaskAction = dcSupTaskActionService.get(taskReceive.getId());
		sysMsgService.makeMsgReadByBiz(tempTaskAction.getTaskId() + "_" + tempTaskAction.getLeaderOrgCode(), "distribute_deploy", null);
		//任务接收后置为已办
		dcSupBacklogService.doBacklog(tempTaskAction.getTaskId(),"task_receive",tempTaskAction.getLeaderOrgCode());
		taskReceive.setTaskReceiveTime(new Date());
		taskReceive.setTaskStatus("2");
		taskReceiveService.update(taskReceive);
		//写入statue表
		dcSupTaskStatusService.addStatus(taskReceive.getId(),"2","2","已接收");
		return renderResult(Global.TRUE, text("接收成功！"));
	}

	/**
	 * 任务接收-反馈保存
	 * @param taskFeedback
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "feedback")
	@ResponseBody
	@RepeatSubmit
	public String feedback(DcSupTaskFeedback taskFeedback){
		String s = JSONObject.toJSONString(taskFeedback);
		String feedback = taskReceiveService.feedback(taskFeedback);
		if(StringUtils.isNotBlank(feedback)){
			return renderResult(Global.FALSE, feedback);
		}
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	/**
	 * 任务接收-工作安排保存
	 * @param taskReceive
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "arrange")
	@ResponseBody
	@RepeatSubmit
	public String arrange(DcSupTaskAction taskReceive) {
		taskReceive.setTransferFlag("1");
		dcSupTaskActionService.update(taskReceive);
		dcSupTaskActionService.updateDecomposeCreateBy(taskReceive.getDecomposeTaskId(),taskReceive.getTransferOrgCode());
		dcSupTaskStatusService.addStatus(taskReceive.getId(),"2","1","转发待接收");
		// 5普任务转办，向转办人发送消息（多次转办，每次都发送消息）
		// 6秘任务转办，向转办人发送消息（多次转办，每次都发送消息）
		// 7细任务转办，向转办人发送消息（多次转办，每次都发送消息）
		DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskFeedbackService.getInfoByActionId(taskReceive.getId());
//		PcMsgContent msg = new PcMsgContent();
//		msg.setTitle("提示信息");
//		// 任务部署
//		msg.setContent("您有一个任务:\""+dcSupTaskInfoRes.getTaskTitle()+"\"待反馈,请及时查看");
//		msg.addButton("任务待办", "/a/action/dcSupTaskAction/noCompleteView");
//		MsgPushUtils.push(msg, dcSupTaskInfoRes.getId() + "_" + taskReceive.getLeaderOrgCode(), "feedback_msg", taskReceive.getTransferOrgCode());
		//发送易政网消息
		MsgDetail msgDetail = new MsgDetail();
		msgDetail.setSendid(UserUtils.getUser().getUserCode());
		msgDetail.setSubjectid(taskReceive.getTransferOrgCode());
		msgDetail.setMesg("您有一个任务:\""+dcSupTaskInfoRes.getTaskTitle()+"\"待反馈,请及时登录系统查看");
		lehandYzwPushTask.pushYzwShortMsg(msgDetail);

		//清掉之前的转办待办
		dcSupBacklogService.doBacklog(taskReceive.getId(),"task_transfer");
		//新增待办
		dcSupBacklogService.add("督查督办","任务待办","转办待反馈","提示信息"
				,"您有一个任务:" + dcSupTaskInfoRes.getTaskTitle() + "待反馈,请及时查看"
				,"/a/action/dcSupTaskAction/noCompleteView",""
				,taskReceive.getId(),"task_transfer",taskReceive.getTransferOrgCode()
				,dcSupTaskInfoRes.getId(),taskReceive.getId());
		//定时任务的代办置为已办
		dcSupBacklogService.doBacklog(taskReceive.getId(),"task_yuqi");

		return renderResult(Global.TRUE, text("工作安排成功！"));
	}
	/**
	 * 任务接收-修改完成进度
	 * @param taskReceive
	 * @return
	 */
	@RequestMapping(value = "updateRate")
	@ResponseBody
	public String updateRate(DcSupTaskFeedback taskReceive) {
		dcSupTaskFeedbackService.update(taskReceive);
		if("1".equals(taskReceive.getFeedbackProgress())||"2".equals(taskReceive.getFeedbackProgress())){
			//设置之后的大于此次的反馈截止时间的反馈状态为-1未反馈
			dcSupTaskFeedbackService.updateLastFeedback("-1",taskReceive.getId());
			//设置action表状态为3进行中
			if(taskReceive.getFeedbackProgress().equals("1")){//反馈的序时推进
				dcSupTaskFeedbackService.updateActionStatus2("3","3",taskReceive.getId());
			}
			if(taskReceive.getFeedbackProgress().equals("2")){//反馈的滞后时序
				dcSupTaskFeedbackService.updateActionStatus2("3","1",taskReceive.getId());
			}
		}else{
			//设置之后的大于此次的反馈截止时间的反馈状态为11已终止
			dcSupTaskFeedbackService.updateLastFeedback("11",taskReceive.getId());
			//设置action表状态为4已结束
			if(taskReceive.getFeedbackProgress().equals("3")){//反馈的已完成
				dcSupTaskFeedbackService.updateActionStatus2("4","4",taskReceive.getId());
			}
			if(taskReceive.getFeedbackProgress().equals("4")){//反馈的未完成
				dcSupTaskFeedbackService.updateActionStatus2("4","5",taskReceive.getId());
			}
		}

		return renderResult(Global.TRUE, text("修改完成进度成功！"));
	}

	/**
	 * 领导批示
	 * @param taskReceive
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "instructions")
	@ResponseBody
	public String instructions(DcSupTaskAction taskReceive) {
		taskReceive.setExplainFlag("1");
		dcSupTaskActionService.update(taskReceive);
		// 4普任务批示，向批示人发送消息
		// 5秘任务批示，向批示人发送消息
		// 6细任务批示，向批示人发送消息

		DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskFeedbackService.getInfoByActionId(taskReceive.getId());
//		PcMsgContent msg = new PcMsgContent();
//		msg.setTitle("提示信息");
//		// 任务部署
//		msg.setContent("您有一个任务:\""+dcSupTaskInfoRes.getTaskTitle()+"\"待批示,请及时查看");
//		msg.addButton("待办批示", "/a/action/dcSupTaskAction/lists");
//		MsgPushUtils.push(msg, taskReceive.getId(), "renwupishi", taskReceive.getExplainCode());
		//发送易政网消息
		MsgDetail msgDetail = new MsgDetail();
		msgDetail.setSendid(UserUtils.getUser().getUserCode());
		msgDetail.setSubjectid(taskReceive.getExplainCode());
		msgDetail.setMesg("您有一个任务:\""+dcSupTaskInfoRes.getTaskTitle()+"\"待批示,请及时登录系统查看");
		lehandYzwPushTask.pushYzwShortMsg(msgDetail);

		//新增待办
		dcSupBacklogService.add("督查督办","待办批示","单位领导待批示","提示信息"
				,"您有一个任务:"+dcSupTaskInfoRes.getTaskTitle()+"待批示,请及时查看"
				,"/a/action/dcSupTaskAction/lists",""
				,taskReceive.getId(),"task_pishi",taskReceive.getExplainCode()
				,dcSupTaskInfoRes.getId(),taskReceive.getId());

		return renderResult(Global.TRUE, text("申请批示成功！"));
	}

	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "exportData")
	public void exportData(TaskReceiveDto taskReceive,HttpServletRequest request, HttpServletResponse response) {
		Page<TaskReceiveDto> page = this.listData(taskReceive,request,response);
		List<TaskReceiveDto> list = page.getList();
		String fileName = "任务接收" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("任务接收", TaskReceiveDto.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * time<time2返回true,否则返回false
	 * 时间比较
	 * @param time
	 * @param time2
	 * @return
	 */

	public static Boolean DateCompare(Date time,Date time2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time2);
		int now_y = calendar.get(Calendar.YEAR);//得到年份
		int now_m = calendar.get(Calendar.MONTH) + 1;//得到月份
		int now_d = calendar.get(Calendar.DATE);//得到月份中今天的号数
		calendar.setTime(time);
		int y = calendar.get(Calendar.YEAR);//得到年份
		int m = calendar.get(Calendar.MONTH) + 1;//得到月份
		int d = calendar.get(Calendar.DATE);//得到月份中今天的号数
		System.out.println("时间是：" + y + "-" + m + "-" + d);
		if (now_y < y || (now_y == y && now_m < m) || (now_y == y && now_m == m && now_d <= d)) {
		} else {
			return true;
		}
		return false;
	}

	/**
	 * 牵头领导批示
	 */
	//@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "getLeaderExplainView")
	public String getLeaderExplainView(DcSupTaskActionExplain dcSupTaskActionExplain, Model model) {
		model.addAttribute("dcSupTaskActionExplain", dcSupTaskActionExplain);
		return "modules/task/receive/taskReceiveLeaderExplain";
	}

	/**
	 * 查看牵头领导批示内容
	 */
	//@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "getLeaderExplain")
	@ResponseBody
	public Page<DcSupTaskActionExplain> getLeaderExplain(DcSupTaskActionExplain dcSupTaskActionExplain, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskActionExplain.setPage(new Page<>(request,response));
		Page<DcSupTaskActionExplain> page = taskReceiveService.getLeaderExplain(dcSupTaskActionExplain);
		return page;
	}

	/**
	 * 手机端taskFeedbackForm查询
	 * @param feedback
	 * @return
	 */
	@RequestMapping(value = "formDetail")
	@ResponseBody
	public String formDetail(DcSupTaskFeedback feedback) {
		DcSupTaskFeedback dcSupTaskFeedbackRes = dcSupTaskFeedbackService.getOverRide(feedback);
		dcSupTaskFeedbackRes.setFeedbackEndTimeStr(DateUtils.formatDate(dcSupTaskFeedbackRes.getFeedbackEndTime(),"yyyy-MM-dd HH:mm"));
		return renderResult(Global.TRUE, text("成功！"),dcSupTaskFeedbackRes);
	}
	/**
	 * 手机端taskFeedbackForm查询
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "formInfo")
	@ResponseBody
	public String formInfo(String taskId) {
		//返回右边数据
		DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
		dcSupTaskInfo.setId(taskId);
		DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskInfoService.get(dcSupTaskInfo);
		dcSupTaskInfoRes.setRemarks2(OfficeUtil.getTaskRequire(dcSupTaskInfoRes.getFeedbackWay(),dcSupTaskInfoRes.getFeedbackRate()));
		return renderResult(Global.TRUE, text("成功！"),dcSupTaskInfoRes);
	}

	@RequestMapping(value = "cuiban")
	@ResponseBody
	@RepeatSubmit
	public String cuiban(String id) {
		taskReceiveService.cuiban(id);
		return renderResult(Global.TRUE, text("催办成功!"));
	}

	@RequestMapping(value = "cuiban2")
	@ResponseBody
	@RepeatSubmit
	public String cuiban2(String id) {
		taskReceiveService.cuiban2(id);
		return renderResult(Global.TRUE, text("催办成功!"));
	}

	@RequestMapping(value = "updateFeedbackTime")
	@ResponseBody
	public String updateFeedbackTime(DcSupTaskFeedback feedback){
		taskReceiveService.updateFeedbackTime(feedback);
		return renderResult(Global.TRUE, text("修改成功,请刷新页面!"));
	}

	@RequestMapping(value = "deleteFeedback")
	@ResponseBody
	public String deleteFeedback(DcSupTaskFeedback feedback){
		dcSupTaskFeedbackService.delete(feedback);
		return renderResult(Global.TRUE, text("删除成功,请刷新页面!"));
	}

	/**
	 * 任务接收-省办任务反馈保存
	 * @param
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "feedbackProvince")
	@ResponseBody
	@RepeatSubmit
	public String feedbackProvince(DcSupTaskFeedbackProvinceExt dcSupTaskFeedbackProvince) {

		try {
			taskReceiveService.feedbackProvince(dcSupTaskFeedbackProvince);
		} catch (Exception e) {
			return renderResult(Global.FALSE, e.getMessage());
		}
		return renderResult(Global.TRUE, text("保存成功！"));
	}


	@RequestMapping(value = "taskRejectOne")
	public String taskRejectOne(DcSupTaskFeedback taskReceive, Model model) {
		//获取反馈表数据
			DcSupTaskFeedback dcSupTaskFeedback = new DcSupTaskFeedback();
			dcSupTaskFeedback.setId(taskReceive.getId());
			DcSupTaskFeedback dcSupTaskFeedbackRes = dcSupTaskFeedbackService.getOverRide(dcSupTaskFeedback);
			//返回右边数据
			DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
			dcSupTaskInfo.setId(dcSupTaskFeedbackRes.getTaskId());
			DcSupTaskInfo dcSupTaskInfoRes = dcSupTaskInfoService.get(dcSupTaskInfo);
			//看是省办数据是否存在
			String platformJson = dcSupTaskInfoRes.getPlatformJson();

			if (StringUtils.isNotBlank(platformJson)) {
				Map map = (Map) JSONObject.parse(platformJson);
				String bizId = (String) map.get("bizId");
				DcSupTaskFeedbackReject dcSupTaskFeedbackReject = new DcSupTaskFeedbackReject();
				dcSupTaskFeedbackReject.setBizId(bizId);
				dcSupTaskFeedbackReject.setActionId(dcSupTaskFeedbackRes.getActionId());
				dcSupTaskFeedbackReject.setTaskId(dcSupTaskFeedbackRes.getTaskId());
				dcSupTaskFeedbackReject.setId(dcSupTaskFeedbackRes.getId());
				dcSupTaskFeedbackReject.setOpTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
				dcSupTaskFeedbackReject.setHandleDeptName(OfficeUtil.getCurrentOffice().getOfficeName());
				dcSupTaskFeedbackReject.setOperTel(UserUtils.getUser().getMobile());
				dcSupTaskFeedbackReject.setOperName(UserUtils.getUser().getUserName());
				model.addAttribute("dcSupTaskFeedbackReject", dcSupTaskFeedbackReject);

			}
		     return "modules/task/receive/taskReceiveReject";
		}


	/**
	 * 省办任务申请退回保存
	 * @param
	 * @return
	 */
	@RequiresPermissions("audit:taskReceive:edit")
	@RequestMapping(value = "feedbackReject")
	@ResponseBody
	@RepeatSubmit
	public String feedbackReject(DcSupTaskFeedbackReject dcSupTaskFeedbackReject ) {

		try {
			taskReceiveService.feedbackReject(dcSupTaskFeedbackReject);
		} catch (Exception e) {
			return renderResult(Global.FALSE, e.getMessage());
		}
		return renderResult(Global.TRUE, text("保存成功！"));
	}


}